import java.io.File;
import java.io.IOException;

import org.openrdf.OpenRDFException;
import org.openrdf.model.Value;
import org.openrdf.query.BindingSet;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.query.UnsupportedQueryLanguageException;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.sail.SailRepository;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFParseException;
import org.openrdf.rio.UnsupportedRDFormatException;
import org.openrdf.sail.nativerdf.NativeStore;


public class Main {
	
	Repository myRepository;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Main great = new Main ();
		great.setRepository();
		great.addTriplets();
		great.request();
	}
	
	void setRepository () {
		File dataDir = new File("/home/lucien/sesame");
		myRepository = new SailRepository(new NativeStore(dataDir));
		// Repository myRepository = new SailRepository (new MemoryStore(dataDir));
		// Repository myRepository = new SailRepository(new MemoryStore());
		try {
			myRepository.initialize();
		} catch (RepositoryException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	void addTriplets () {
		File file = new File ("/home/lucien/www/dbpedia/extraction/triples.n3");
		String baseURI = "";

		try {
			RepositoryConnection con = myRepository.getConnection ();
			try {
				con.add (file, baseURI, RDFFormat.N3);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace ();
			} catch (UnsupportedRDFormatException e) {
				System.out.println ("UnsupportedRDFormatException - If no parser is available for the specified RDF format.");
			} catch (RDFParseException e) {
				System.out.println ("RDFParseException - If an error was found while parsing the RDF data.");
			} catch (RepositoryException e) {
				System.out.println ("RepositoryException - If the data could not be added to the repository, for example because the repository is not writable.");
			} finally {
				con.close ();
			}
		} catch (OpenRDFException e) {
			   // handle exception
		} catch (Exception e) {
			System.out.println ("/!\\");
		}
	}
	
	void request () {
		try {
			RepositoryConnection con = myRepository.getConnection();
			try {
				String queryString = "SELECT ?a ?b ?c\n" +
					"WHERE {\n" +
					"?a ?b ?c.\n" +
					//"?b <http://ishango.cc/name> ?a .\n" +
					"}";
				TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
				TupleQueryResult result = tupleQuery.evaluate();
				try {
					while (result.hasNext()) {
						BindingSet bindingSet = result.next();
						
						//Value va = bindingSet.getValue("a");
						Value vb = bindingSet.getValue("b");
						//System.out.println (va.toString() + " : " + vb.toString());
						System.out.println (vb.toString());
					}
				} finally {
					result.close();
				}
			} catch (IllegalArgumentException e) {
				System.out.println ("IllegalArgumentException - If the supplied query is not a tuple query.");
			} catch (MalformedQueryException e) {
				System.out.println ("MalformedQueryException - If the supplied query is malformed.");
			} catch (UnsupportedQueryLanguageException e) {
				System.out.println("UnsupportedQueryLanguageException - If the supplied query language is not supported.");
			} catch (RepositoryException e) {
				System.out.println("RepositoryException");
			} finally {
				con.close();
			}
		} catch (OpenRDFException e) {
			   // handle exception
		}
	}
}
